import 'package:flutter/material.dart';

class CombinationWidgetRoute extends StatelessWidget {
  const CombinationWidgetRoute({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('组合组件'),
      ),
      body: Container(
        padding: const EdgeInsets.all(20),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            GradientButton(
              colors: const [Colors.orange, Colors.red],
              child: const Text("Submit"),
              onPressed: onTap,
            ),
            const SizedBox(height: 20),
            GradientButton(
              height: 50.0,
              colors: [Colors.lightGreen, Colors.green.shade700],
              child: const Text("Submit"),
              onPressed: onTap,
            ),
            const SizedBox(height: 20),
            GradientButton(
              height: 50.0,
              borderRadius: const BorderRadius.all(Radius.circular(5)),
              colors: [Colors.lightBlue.shade300, Colors.blueAccent],
              child: const Text("Submit"),
              onPressed: onTap,
            ),
          ],
        ),
      ),
    );
  }

  onTap() {
    print("button click");
  }
}

class GradientButton extends StatelessWidget {
  final List<Color>? colors;
  final double? width;
  final double? height;
  final BorderRadius? borderRadius;
  final GestureTapCallback? onPressed;
  final Widget child;

  const GradientButton(
      {Key? key,
      this.colors,
      this.width,
      this.height,
      this.borderRadius,
      this.onPressed,
      required this.child})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    var theme = Theme.of(context);
    List<Color> _colors =
        colors ?? [theme.primaryColor, theme.primaryColorDark];
    return DecoratedBox(
      decoration: BoxDecoration(
        gradient: LinearGradient(colors: _colors),
        borderRadius: borderRadius,
      ),
      child: Material(
        type: MaterialType.transparency,
        child: InkWell(
          splashColor: _colors.last,
          highlightColor: Colors.transparent,
          borderRadius: borderRadius,
          onTap: onPressed,
          child: ConstrainedBox(
            constraints: BoxConstraints.tightFor(height: height, width: width),
            child: Center(
              child: Padding(
                padding: const EdgeInsets.all(8),
                child: DefaultTextStyle(
                  child: child,
                  style: const TextStyle(fontWeight: FontWeight.bold),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
